<template>
  <view class="container">
    <u-navbar
      :is-fixed="false"
      :border-bottom="false"
      :is-back="true"
      back-icon-name="arrow-leftward"
      title="商品详情"
      :background="{ background: '#fff' }"
      title-color="#000000"
    >
    </u-navbar>
    <u-popup v-model="show" mode="bottom" border-radius="14">
      <view class="popup">
        <view class="flex-between">
          <view>数量</view>
          <u-number-box v-model="cnt"></u-number-box>
        </view>
        <view class="full-btn" @click="addCart">加入购物车</view>
      </view>
		</u-popup>
    <u-toast ref="uToast" />
    <view class="content">
      <u-swiper height="700" :list="swipers"></u-swiper>
      <view class="name">{{ good.name }}</view>
      <view class="maijia" hover-class="none" hover-stop-propagation="false">
        <text style="font-size: 30rpx;margin-top: 8rpx;">卖家：</text>
        <text style="font-size: 38rpx;">{{ good.user.name }}</text>
      </view>
      <view class="price" hover-class="none" hover-stop-propagation="false">
        <text style="font-size: 30rpx;margin-top: 8rpx;">价格：</text>
        <text style="font-size: 30rpx;margin-top: 8rpx;color: red;">¥ </text>
        <text style="font-size: 38rpx;color: red;">{{ good.price }}</text>
      </view>
      <view style="margin: 18rpx;" hover-class="none" hover-stop-propagation="false">
        发货地：{{good.delivery}}
      </view>
      <view class="desc" style="margin-top: 40rpx;">{{ good.desc }}</view>
    </view>
    <view class="pay">
      <view class="cart" @click="showModal">加入购物车</view>
    </view>
  </view>
</template>

<script>
import { getMenu } from "../../api/system/api";
import moment from "moment";
export default {
  data() {
    return {
      userInfo: {},
      good: {},
      swipers: [],
      imgUrl: "http://127.0.0.1:3000/",
      cnt: 0,
      show: false,
    };
  },
  onLoad(option) {
    this.good = JSON.parse(option.good);
    console.log(this.good)
    if (this.good.imgs && typeof this.good.imgs) {
      this.swipers = JSON.parse(this.good.imgs).map(v => {
        return {
          image: this.imgUrl + v
        }
      })
    }
    uni.getStorage({
      key: "userInfo",
      success: (res) => {
        this.userInfo = res.data;
      },
      error: (res) => {
      },
    });
  },
  methods: {
    getData() {
      getMenu({
        page: 1,
        pageSize: 100,
        name: this.key,
      }).then((res) => {
        this.goodList = res.data.data;
      });
    },
    addCart() {
      if (!this.cnt) {
        this.$refs.uToast.show({
          title: "请选择数量~",
          type: "error",
        });
        return
      }
      let cartList = {}
      // 获取cartList的缓存
      uni.getStorage({
        key: "cartList",
        success: (res) => {
          cartList = res.data
        },
        error: (res) => {
        },
      });
      setTimeout(() => {
        if (!Object.keys(cartList).length) {
          cartList[this.userInfo.id] = [{
            cnt: this.cnt,
            goodId: this.good.id,
            good: this.good
          }]
          uni.setStorage({
					  key:'cartList',
					  data: cartList,
					  success: () => {
              this.$refs.uToast.show({
                title: "已成功加入购物车~",
                type: "success",
              });
              this.show = false;
					  }
					})
        } else {
          let currentUserGood = cartList[this.userInfo.id]
          if (currentUserGood) {
            let good = currentUserGood.find(v => v.goodId == this.good.id)
            if (good) {
              good.cnt = good.cnt + this.cnt
            } else {
              cartList[this.userInfo.id].push({
                cnt: this.cnt,
                goodId: this.good.id,
                good: this.good
              })
            }
            
          } else {
            cartList[this.userInfo.id] = [{
              cnt: this.cnt,
              goodId: this.good.id,
              good: this.good
            }]
          }
          uni.setStorage({
            key:'cartList',
            data: cartList,
            success: () => {
              this.$refs.uToast.show({
                title: "已成功加入购物车~",
                type: "success",
              });
              this.show = false;
            }
          })
        }
      }, 1000)
    },
    showModal() {
      if (this.userInfo.id) {
        this.show = true;
        this.cnt = 0;
      } else {
        this.$refs.uToast.show({
          title: "请先登录~",
          type: "error",
        });
      }
    }
  },
};
</script>

<style lang="scss">
.container {
  min-height: 100vh;
  overflow: hidden;
  background: #fff3f3;
}
.cover {
  width: 160rpx;
  height: 180rpx;
}
.each-good {
  width: 100%;
  height: 200rpx;
  background: #fff;
  padding: 12rpx 20rpx;
  display: flex;
  margin-top: 20rpx;
  justify-content: space-between;

  .info {
    width: calc(100% - 200rpx);
    position: relative;
    .icon {
      position: absolute;
      right: 20rpx;
      top: 30%;
    }
  }
  .title {
    font-size: 30rpx;
    font-weight: bold;
    margin-bottom: 10rpx;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 1;
    overflow: hidden;
  }
  .price {
    color: red;
    display: flex;
    margin-bottom: 10rpx;
  }
  .desc {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 3;
    overflow: hidden;
    font-size: 22rpx;
  }
}
.name {
  width: 96%;
  margin: 10rpx 2%;
  font-weight: bold;
  font-size: 30rpx;
  text-align: center;
}
.pay {
	width: 100%;
	position: fixed;
	bottom: 0;
	height: 100rpx;
	padding: 12rpx 20rpx;
	display: flex;
	background: #fff;
  padding-top: 20rpx;
  justify-content: flex-end;
  .cart {
    padding: 10rpx 16rpx;
    height: 60rpx;
    line-height: 40rpx;
    text-align: center;
    color: #fff;
    background: #ff9900;
    border-radius: 30rpx;
  }
}
.desc,
.price {
  width: 96%;
  margin: 10rpx 2%;
  line-height: 30rpx;
  color: #333;
  font-size: 26rpx;
}
.maijia {
  width: 96%;
  margin: 10rpx 2%;
  line-height: 30rpx;
  color: #333;
  font-size: 20rpx;
  margin-bottom: 12rpx;
}
.popup{
  padding: 40rpx 30rpx;
}
.full-btn {
  width: 96%;
  margin: 30rpx 2%;
  background: red;
  border-radius: 30rpx;
  text-align: center;
  color: #fff;
  padding: 20rpx 40rpx;
}
.flex-between {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
</style>
